/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database;

import configuration.ConfigServer;
import configuration.ServerConfig;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import javax.swing.JOptionPane;


/**
 *
 * @author anhquynh
 */
public class DataConnect {
    //ConnectDB will Connect to Database SQL Server by use jdbc driver.
    public static Connection connectDB(){
        Connection conn = null;
        ServerConfig cfserver = config();
        if(cfserver==null){
            JOptionPane.showMessageDialog(null,"Cannot connect database, please config data server");
            ConfigServer conf = new ConfigServer();
            conf.show();
            return null;
        }
        else{
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url = "jdbc:sqlserver://"+cfserver.getServerName()+":"+cfserver.getPort()+";databaseName="+cfserver.getDatabasename();
            conn = DriverManager.getConnection(url,cfserver.getUserName(),cfserver.getPassword());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return conn;
        }
    }
    //Read file data connect, if they have a connect without a second set.
    public static ServerConfig config(){
        File file = new File("config.properties");
        ServerConfig con = new ServerConfig();
        try {
            if(!file.exists()){
                return null;
            }
            FileInputStream fis = new FileInputStream(file);
            Properties pro = new Properties();
            pro.load(fis);
            con.setServerName(pro.getProperty("servername"));
            con.setDatabasename(pro.getProperty("databasename"));
            con.setPort(pro.getProperty("port"));
            con.setUserName(pro.getProperty("username"));
            con.setPassword(pro.getProperty("password"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }
    //Output file save all data connect if connect successfully.
    public static void setConfig(ServerConfig con){
        File file = new File("config.properties");
        Properties pro = new Properties();
        try {
            if(!file.exists()){
                file.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(file);
            pro.setProperty("servername",con.getServerName() );
            pro.setProperty("databasename",con.getDatabasename() );
            pro.setProperty("port", con.getPort());
            pro.setProperty("username",con.getUserName() );
            pro.setProperty("password", con.getPassword());
            pro.store(fos,"");
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean isValidConnection(Connection conn) {
        try {
            if (conn == null) {
                return false;
            } else if (conn.isClosed()) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static void closeConnection(Connection conn) throws SQLException {
        try {
            if (!isValidConnection(conn)) {
                conn.close();
            }
        } catch (NullPointerException e) {}
    }
}
